package com.epcm.admin.epcm.project.controller;

import com.epcm.admin.common.core.web.BaseController;
import com.epcm.admin.epcm.project.service.ProjectService;
import com.epcm.admin.epcm.project.entity.Project;
import com.epcm.admin.epcm.project.param.ProjectParam;
import com.epcm.admin.common.core.web.ApiResult;
import com.epcm.admin.common.core.web.PageResult;
import com.epcm.admin.common.core.web.PageParam;
import com.epcm.admin.common.core.web.BatchParam;
import com.epcm.admin.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目清单控制器
 *
 * @author linktoyl@163.com
 * @since 2023-10-18 01:39:33
 */
@Api(tags = "项目清单管理")
@RestController
@RequestMapping("/api/epcm/project")
public class ProjectController extends BaseController {
    @Resource
    private ProjectService projectService;

    @PreAuthorize("hasAuthority('epcm:project:list')")
    @OperationLog
    @ApiOperation("分页查询项目清单")
    @GetMapping("/page")
    public ApiResult<PageResult<Project>> page(ProjectParam param) {
        PageParam<Project, ProjectParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        return success(projectService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(projectService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('epcm:project:list')")
    @OperationLog
    @ApiOperation("查询全部项目清单")
    @GetMapping()
    public ApiResult<List<Project>> list(ProjectParam param) {
        PageParam<Project, ProjectParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        return success(projectService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(projectService.listRel(param));
    }

    @PreAuthorize("hasAuthority('epcm:project:list')")
    @OperationLog
    @ApiOperation("根据id查询项目清单")
    @GetMapping("/{id}")
    public ApiResult<Project> get(@PathVariable("id") Integer id) {
        return success(projectService.getById(id));
        // 使用关联查询
        //return success(projectService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('epcm:project:save')")
    @OperationLog
    @ApiOperation("添加项目清单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Project project) {
        if (projectService.save(project)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('epcm:project:update')")
    @OperationLog
    @ApiOperation("修改项目清单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Project project) {
        if (projectService.updateById(project)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('epcm:project:remove')")
    @OperationLog
    @ApiOperation("删除项目清单")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (projectService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('epcm:project:save')")
    @OperationLog
    @ApiOperation("批量添加项目清单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Project> list) {
        if (projectService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('epcm:project:update')")
    @OperationLog
    @ApiOperation("批量修改项目清单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Project> batchParam) {
        if (batchParam.update(projectService, "project_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('epcm:project:remove')")
    @OperationLog
    @ApiOperation("批量删除项目清单")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (projectService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
